package net.midnighttd.engine.view;

import net.midnighttd.engine.map.Map;
import net.midnighttd.engine.map.Tile;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Author: Adam Scarr
 * Date: 17/04/2009
 * Time: 7:23:55 PM
 *
 * Used to render a map.
 */
public class MapRenderer implements Layer {
    private Map map;

    public MapRenderer(Map map) {
        this.map = map;
    }

    public void paintLayer(Graphics g) {
        assert(map.getHeight() > 0);
        assert(map.getWidth() > 0);
        BufferedImage tile;
        int mapX = 0;
        int mapY = 0;
        int screenX = 0;
        int screenY = 0;
        for (Iterable<Tile> row : map) {
            //noinspection UnusedDeclaration
            for (Tile cell : row) {
                tile = map.getTileset().getTile(map, mapX, mapY);

                g.drawImage(tile, screenX, screenY, null);
//                g.setColor((new Color(161,157,154)));
//                g.drawRect(screenX, screenY, map.getTileset().getTilewidth(), map.getTileset().getTileheight());
                mapX++;
                screenX += map.getTileset().getTilewidth();
            }
            mapY++;
            mapX = 0;
            screenY += map.getTileset().getTileheight();
            screenX = 0;
        }
    }

    public int getWidth() {
        return map.getTileset().getTilewidth() * map.getWidth();
    }

    public int getHeight() {
        return map.getTileset().getTileheight() * map.getHeight();
    }
}
